package com.tjetc.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.dao.NewsDao;
import com.tjetc.dao.impl.NewsDaoImpl;
import com.tjetc.domain.News;
import com.tjetc.service.NewsService;
import com.tjetc.utils.JedisDb;
import redis.clients.jedis.Jedis;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.*;

public class NewsServiceImpl implements NewsService {
    private NewsDao newsDao=new NewsDaoImpl();

    private Jedis jedis = JedisDb.jedis();
    @Override
    public List<News> findBySid(int sid) {
        try {
            return newsDao.findBySid(sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount(String content) {
        try {
            return newsDao.getCount(content);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<News> list(String content, PageInfo<News> pageInfo) {
        try {
            return newsDao.list(content,pageInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean del(int id) {
        try {
            return newsDao.del(id)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean add(News news) {
        try {
            return newsDao.add(news)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean update(News news) {
        try {
            return newsDao.update(news)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public Set<String> listHotNews() {
        try {
            String key = "hot_news";
            ObjectMapper mapper = new ObjectMapper();
            Set<String> hotNews = jedis.zrevrange(key, 0, -1);
            System.out.println("hotNews = " + hotNews);
            //SortedSet<News> sets = new TreeSet<>();
            Set<News> sets = new HashSet<>();
            if (hotNews!=null&&hotNews.size()>0){
                return hotNews;
                /*for (String json : hotNews) {
                    News news = mapper.readValue(json, News.class);
                    sets.add(news);
                }
                return sets;*/
            }else{//从redis中，查询数据库
                List<News> newsList = newsDao.listHotNews();
                //存入redis中
                if (newsList!=null&&newsList.size()>0){
                    for (News news : newsList) {
                        jedis.zadd(key,0,mapper.writeValueAsString(news));
                        //设置过期时间,一天之后过期
                        jedis.expire(key,24*60*60L);
                    }
                }else{//缓存空值
                    jedis.zadd(key,0,"");
                }
                return listHotNews();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean addScore(int id) {
        try {
            String key = "hot_news";
            ObjectMapper mapper = new ObjectMapper();
            Set<String> hotNews = jedis.zrevrange(key, 0, -1);
            for (String json : hotNews) {//这里redis中肯定是有数据的，不然根本点不了新闻，而后触发不了方法
                News news = mapper.readValue(json, News.class);
                if (news.getId() == id){//说明是我要加分的新闻
                    jedis.zincrby(key,1,mapper.writeValueAsString(news));
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

}
